﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApqDBManager.Controls;

namespace ApqDBManager.Forms
{
	public partial class SqlOut : Apq.Windows.Forms.DockForm
	{
		private SqlEdit _SqlEdit = null;
		public SqlEdit SqlEdit
		{
			get { return _SqlEdit; }
			set { _SqlEdit = value; }
		}

		public SqlOut()
		{
			InitializeComponent();

			lstds = new Apq.Collections.IListT<DataSet>(_lstds);
			lstds.ListChanged += new ListChangedEventHandler(lstds_ListChanged);
		}

		private List<DataSet> _lstds = new List<DataSet>();
		private Apq.Collections.IListT<DataSet> lstds = null;
		/// <summary>
		/// 获取结果集列表
		/// </summary>
		public Apq.Collections.IListT<DataSet> lstRts
		{
			get { return lstds; }
		}

		#region ResultTableLocks
		private Dictionary<string, object> _ResultTableLocks;
		/// <summary>
		/// 结果表格控件锁对象集合,名称为ServerID(供lock使用)
		/// </summary>
		public Dictionary<string, object> ResultTableLocks
		{
			get
			{
				if (_ResultTableLocks == null)
				{
					_ResultTableLocks = new Dictionary<string, object>();
				}
				return _ResultTableLocks;
			}
		}

		/// <summary>
		/// 获取控件锁
		/// </summary>
		/// <returns></returns>
		public object GetLock(string ServerID)
		{
			lock (ResultTableLocks)
			{
				if (!ResultTableLocks.ContainsKey(ServerID))
				{
					object obj = new object();
					ResultTableLocks.Add(ServerID, obj);
				}
				return ResultTableLocks[ServerID];
			}
		}
		#endregion

		private void SqlOut_Load(object sender, EventArgs e)
		{
		}

		private void lstds_ListChanged(object sender, ListChangedEventArgs e)
		{
			if (e.ListChangedType == ListChangedType.Reset)
			{
				lock (LockUI)
				{
					// 清空结果窗口
					Apq.Windows.Delegates.Action_UI<TabControl>(tc, tc, delegate(TabControl ctrl)
					{
						tc.TabPages.Clear();
					});
				}
			}

			if (e.ListChangedType == ListChangedType.ItemAdded)
			{
			}
		}

		#region cms2
		//定位对应节点
		private void tsmiShowNode_Click(object sender, EventArgs e)
		{
			//SqlEdit.FocusAndExpandByID(Apq.Convert.ChangeType<int>(tc.SelectedTab.Tag));
		}
		#endregion

		#region UI 公开方法
		/// <summary>
		/// 添加TabPage页面
		/// </summary>
		/// <param name="Name"></param>
		public TabPage AddTabPage(int DBIID, string DBIName)
		{
			lock (LockUI)
			{
				TabPage tp = null;

				Apq.Windows.Delegates.Action_UI<TabControl>(tc, tc, delegate(TabControl ctrl)
				{
					tp = new TabPage(DBIName);
					tc.TabPages.Add(tp);
					tp.Tag = DBIID;

					ResultTable rt = new ResultTable();
					tp.Controls.Add(rt);

					rt.Dock = System.Windows.Forms.DockStyle.Fill;
					rt.Location = new System.Drawing.Point(0, 0);
					rt.Margin = new System.Windows.Forms.Padding(0);

					rt.dsBack.ExtendedProperties["__DBIID"] = DBIID;
					rt.dsBack.DataSetName = DBIName;
				});

				return tp;
			}
		}

		// 在指定页添加表
		public void AddTable(TabPage tp, DataTable dt)
		{
			ResultTable rt = tp.Controls[0] as ResultTable;
			if (rt != null)
			{
				rt.dsBack.Merge(dt);

				lstds.AddUnique(rt.dsBack);
			}
		}

		/*
		// 在指定页添加消息
		public void AddMsg(TabPage tp, Apq.Windows.Forms.DockForms.ErrList_XSD.ErrListRow dr)
		{
			ResultTable rt = tp.Controls[0] as ResultTable;
			if (rt != null)
			{
				rt.AddMsg(dr);
			}
		}
		*/

		/// <summary>
		/// 显示指定名称的结果页
		/// </summary>
		/// <param name="Name"></param>
		public void ShowTabPage(string DBIName)
		{
			foreach (TabPage tp in tc.TabPages)
			{
				if (tp.Text == DBIName)
				{
					tc.SelectedTab = tp;
					return;
				}
			}
		}

		/*
		/// <summary>
		/// 获取当前有结果显示的名称列表
		/// </summary>
		/// <returns></returns>
		public string[] GetCurrentResultNames()
		{
			string[] aryNames = new string[tc.TabPages.Count];
			for (int i = 0; i < aryNames.Length; i++)
			{
				aryNames[i] = tc.TabPages[i].Text;
			}
			return aryNames;
		}
		 * */
		#endregion
	}
}
